/*
Version 2

Suparerk@

- add showTime : 'hover' for hover action
- fix bug
- tooltips follwow mouse
- fix left position
- fix botoom position


*/

(function($) {
		  
	jQuery.fn.toolTipsTw = function(settings) {
		var tipTime;
		var holder = $(this);
		var defaults = {
			mes: "Click to Show me!",
			tipName:"Tips",
			showTime:5000,
			ajax:false
		}
		
		var options = jQuery.extend(defaults, settings);
		
		
		holder.each(function(i) {
			var thisEle = $(this);
			if (options.showTime != "hover"){//if timer
				if(!$("#tips"+options.tipName+i).size()){
					
					$("body").append('<div id="tips'+options.tipName+i+'" style="position:absolute; width:auto; z-index:9999; cursor:pointer;opacity:0.9;filter:Alpha(Opacity=90); display:none" class="ui-widget toolTipsTw"><img src="'+CDNname+'images/tooltipsCorner.png" style="margin-left:5px" /><div style=" background-color:#FFFFCC; padding:5px" class="ui-corner-all"><div class="ui-icon ui-icon-alert"></div><div class="tips">Loading..</div></div><div>');
					
					var leftPo;
					var topPo;
					var distance;
					//check x
					if($(thisEle).offset().left+$("#tips"+options.tipName+i).width() > $(window).width()){
						$("#tips"+options.tipName+i+" img").css({'margin-left':$("#tips"+options.tipName+i).width()/2});
						leftPo = $(thisEle).offset().left-($("#tips"+options.tipName+i).width()/2);
						distance = ($("#tips"+options.tipName+i).width()/2)
					}else{
						leftPo = $(thisEle).offset().left-10;
						if(leftPo < 0 ) {leftPo = 10}
						distance = 10;
					}
					//check y
					if($(thisEle).offset().top+$("#tips"+options.tipName+i).height() > $(window).height()){
						$('#tips'+options.tipName+i).find("img").hide();
						topPo = $(thisEle).offset().top-$(this).height()-$('#tips'+options.tipName+i).height();
					}else{
						topPo = $(thisEle).offset().top+$(this).height();
					}
					
					//set timer
					tipTime = setTimeout('closeTips("'+options.tipName+i+'")', options.showTime);
				
					$("#tips"+options.tipName+i).css({top:topPo, left:leftPo}).show(500,function(){
						//laod content																	 
						if(!options.ajax){
							$('#tips'+options.tipName+i+' div.tips').html(options.mes);
						}else{
							$('#tips'+options.tipName+i+' div.tips').load(options.mes);
						}
						//
						holder.mousemove(function(e){
							$("#tips"+options.tipName+i).css({top:e.pageY+15,left:e.pageX-distance});
						}).mouseout(function(){
							$("#tips"+options.tipName+i).css({top:topPo, left:leftPo})
						});
					}).click(function(){
						clearTimeout(tipTime);
						closeTips(options.tipName+i);
					});
				
					
				}else{
					clearTimeout(tipTime);
					//closeTips(options.tipName+i);
				}
				
			}//if hover
			else{
				
				$(thisEle).hover(function(){
					if(!$("#tips"+options.tipName+i).size()){
						$("body").append('<div id="tips'+options.tipName+i+'" style="position:absolute; width:auto; z-index:9999; cursor:pointer;opacity:0.9;filter:Alpha(Opacity=90);display:none " class="ui-widget toolTipsTw"><img src="'+CDNname+'images/tooltipsCorner.png" style="margin-left:5px; " /><div style=" background-color:#FFFFCC; padding:5px;" class="ui-corner-all"><div class="ui-icon ui-icon-alert"></div><div class="tips">Loading..</div></div><div>');
					}
					
					//check x
					if($(thisEle).offset().left+$("#tips"+options.tipName+i).width() > $(window).width()){
						$("#tips"+options.tipName+i+" img").css({'margin-left':$("#tips"+options.tipName+i).width()/2});
						leftPo = $(thisEle).offset().left-($("#tips"+options.tipName+i).width()/2);
					}else{
						leftPo = $(thisEle).offset().left-10;
						if(leftPo < 0 ) {leftPo = 10}
					}
					//check y
					if($(thisEle).offset().top+$("#tips"+options.tipName+i).height() > $(window).height()){
						$('#tips'+options.tipName+i).find("img").hide();
						topPo = $(thisEle).offset().top-$(this).height()-$('#tips'+options.tipName+i).height();
					}else{
						topPo = $(thisEle).offset().top+$(this).height();
					}
					
					//assign x:y 
					$("#tips"+options.tipName+i).show(500,function(){
						//laod content																	 
						if(!options.ajax){
							$('#tips'+options.tipName+i+' div.tips').html(options.mes);
						}else{
							$('#tips'+options.tipName+i+' div.tips').load(options.mes);
						}		
						
					}).css({top:topPo, left:leftPo});
					
				},function(){
					$("div#tips"+options.tipName+i).hide(500,function(){ $(this).remove()});
				});
				
				
			}
			
			
		});
		
    };
	
 		
})(jQuery);

function closeTips(objName){
	$("div#tips"+objName).hide(1000,function(){ $(this).remove()});
};